<?php


namespace app\library\rpc;


class RpcBase
{
    public static function __callStatic($name, $arguments)
    {
        // TODO: Implement __callStatic() method.

        $className = get_called_class();
        $className = trim(strrchr($className, '\\'), '\\');
        $params = [
            'jsonrpc' => "2.0",
            'method' => "{$className}Interface@{$name}",
            'params' => [...$arguments],
            'id' => '',
        ];

        $pool = \Yaf\Registry::get('rpc_client_pool');
        $client = $pool->borrow();

        $string = json_encode($params, JSON_UNESCAPED_UNICODE);
        //转成二进制数据
        $string = pack('NN', strlen($string), 0) . $string;
        $client->send($string);
        $res = $client->recv();
        $pool->return($client);
        //解 二进制数据包
        $header = unpack('Nlength/Ntype', substr($res, 0, 8));
        $data   = substr($res, 8);

        $data = json_decode($data, true);
        return $data['result'];
    }
}